import requests
import json
import os
import time
import random
import datetime
import sys

#当前文件的路径
pwd = os.getcwd()
sys.path.append(pwd)
project_path=os.path.abspath(os.path.dirname(pwd)+os.path.sep+"..")
sys.path.append(project_path)

from api.rest_api import RestApi

from util.util_function import DownloadFile
from util.log_handler import LogHandler
from util.config import GetConfig

# 获取文件名
def get_filename(path):
	filename = os.path.basename(path)
	filename = filename.split('.')[0]
	return filename

# 获取当前执行程序的文件名
def get_execute_filename():
	return get_filename(__file__)

log = LogHandler(get_execute_filename())

api = RestApi()

# 调用接口
def post_guatian_data(data):
	try:
		r = api.post_guatian_data(data)
		return (json.loads(r)["data"])
	except Exception as e:
		log.error("api request fail: %s"%(e), exc_info=True)

def deal_guatian_info(list_data):
	for data in list_data:
		id = data['id']
		content = json.dumps(data)
		data = {'id': id, 'content':content}
		print(data['id'], json.loads(data['content'])['pictures'][0])
		post_guatian_data(data)

def token_one(token_value=''):
    ''' 请求token值 '''
    url = "http://guatian.jy-tech.com.cn/rest/user/token"
    headers = {
        "token": token_value,
        'api_version': '17',
        "Content-Type": 'application/json; charset=UTF-8',
        "Content-Length": '58',
        "Host": 'guatian.jy-tech.com.cn',
        "Connection": 'Keep-Alive',
        "Accept-Encoding": 'gzip',
        "Cookie": "token: " + token_value,
        "User-Agent": 'okhttp/2.5.0',
    }
    body = {"id": 0, "refreshToken": "8cee816450684edaa2cfb755a2cce5f2"}
    response = requests.post(url=url, headers=headers, data=json.dumps(body))
    response.endcoding = 'utf-8'
    # {"code":0,"payload":{"id":9810,"admin":false,"accessToken":"9810:1568273031191:9feaINBbgfR7+YPlE0WBaA==","refreshToken":"4cc1266c03c140f4a7634b9735d197ba"}}
    ret = json.loads(response.text)
    # print(ret)
    # print(ret['payload']['accessToken'])
    return ret['payload']['accessToken']


def request_json(token_value, i):
    ''' 请求json数据 '''
    base_url = "http://guatian.jy-tech.com.cn/rest/questions/all-communication?currentPage=" + str(i) + "&pageSize=20"
    headers = {
        "Content-Type": "application/json",
        "token": "{}".format(token_value),
        "api_version": "17",
        "Host": "guatian.jy-tech.com.cn",
        "Connection": "Keep-Alive",
        "Accept-Encoding": "gzip",
        "Cookie": "{}={}".format('token', token_value),
        "User-Agent": "okhttp/2.5.0"
    }

    response = requests.get(url=base_url, headers=headers)
    response.endcoding = 'utf-8'
    # print(response.text)
    return response.text

def folder_exist(dir_path):
    '''
    1. 作用:判断文件夹路径是否存在,不存在则创建
    2. 参数:dir_path:文件夹路径
    3. 返回值:None
    '''
    if not os.path.exists(dir_path):
        os.makedirs(dir_path)

def save_json(json_data, i):
    ''' 保存json文件 '''
    today = datetime.datetime.today()
    str_today = datetime.datetime.strftime(today, '%Y%m%d')
    json_dir = base_dir.format(str_today)
    folder_exist(json_dir)
    json_path = os.path.join(json_dir, 'page_{}.json'.format(i))
    with open(json_path.format(str_today, i), 'w', encoding='utf-8') as f:
        print(json_path.format(str_today, i))
        f.write(json_data)
    return "{}:第{}页json存储成功".format(json_dir, i)

def pic_url(json_info):
    pic_list = []
    pic_dict = json.loads(json_info)
    ret = pic_dict["payload"]["list"]
    for i in ret:
        # print("pic", i['pictures'][0])
        pic_list.append(i['pictures'][0])
    return pic_list

def get_yesterday():
    '''
    1. 作用:获取前一天的年月日字符串
    2. 参数:无
    3. 返回值:前一天的年月日
    '''
    today_time = datetime.datetime.today()
    yesterday = today_time + datetime.timedelta(-1)
    str_yesterday = datetime.datetime.strftime(yesterday, "%Y%m%d")
    return str_yesterday

def list_file(path, ext='.json'):
    '''
    1.作用:遍历文件夹下的所有指定后缀名的文件的具体路径
    2.参数： 
        path:需要遍历的目录， 
        ext:文件后缀名，默认为'.jpg'
    3.返回值:文件的绝对路径
    '''
    list_file=[]
    for root, dirs, files in os.walk(path):
        for file in files:
            file_ext = file.endswith(ext)
            if file_ext:
                fpath = os.path.join(root, file)
                list_file.append(fpath)
    return list_file

def get_first_file(dir_path):
    ''' 获取第一个一个文件 '''
    list_filename = list_file(dir_path)
    list_filename.sort()
    return list_filename[0]

def get_json_info(json_file):
    ''' 通过json文件名获取内容 '''
    with open(json_file) as f:
        json_info = json.load(f)
    return json_info

if __name__ == '__main__':
    token_value = '' # 初始的token值
    # base_dir = '/home/ubuntu/workspace/guatian/json_data{}' # 保存数据的目录
    base_dir = '/home/ubuntu/workspace/spider_data/guatian/json_data{}' # 保存数据的目录

    for i in range(100):
        token_value = token_one(token_value)
        json_info = request_json(token_value, i)

        str_yesterday = get_yesterday() # 昨天的年月日
        print(str_yesterday)
        str_yesterday = '20200123'
        dir_yesterday = base_dir.format(str_yesterday) # 昨天下载json文件的目录
        last_file = get_first_file(dir_yesterday) # 第一个文件
        yesterday_json_info = get_json_info(last_file) # 第一个文件的json信息
        yesterday_id = yesterday_json_info['payload']['list'][0]['id'] # 昨天最新的id

        dict_info = json.loads(json_info)
        deal_guatian_info(dict_info['payload']['list']) # 向接口发送数据

        result = save_json(json_info, i) # 保存文件
        print(result)
        # 判断昨天的id是否大于现在爬取的id,如果是那么已经拿到最新的数据了停止请求数据
        if yesterday_id > dict_info['payload']['list'][-1]['id']:
            break
        time.sleep(random.randint(2, 10)) # 随机暂停2-10秒

